package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	import gs.TweenLite;
	
	import org.ilovecode.buttons.FloorLayoutFloorButton;
	import org.ilovecode.buttons.RoundFloorButton;
	import org.ilovecode.events.MenuEvent;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.ScreenGroup;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class FloorLayoutBusiness extends FloorScreen {
		
		//{ region Stage elements
		
		/*public var floorButton0:SimpleButton;
		public var floorButton1:SimpleButton;
		public var floorButton2:SimpleButton;
		public var floorButton3:SimpleButton;
		public var floorButton4:SimpleButton;
		public var floorButton5:SimpleButton;
		public var floorButton6:SimpleButton;
		public var floorButton7:SimpleButton;
		public var floorButton8:SimpleButton;*/
		public var bFloor0:MovieClip;
		public var bFloor1:MovieClip;
		public var bFloor2:MovieClip;
		public var bFloor3:MovieClip;
		public var bFloor4:MovieClip;
		public var bFloor5:MovieClip;
		public var bFloor6:MovieClip;
		public var bFloor7:MovieClip;
		public var bFloor8:MovieClip;
		public var backButton:SimpleButton;
		public var downloadPDFButton:SimpleButton;
		
		//} endregion
		
		private const NUM_FLOORS:int = 8;
		private var currentFloor:MovieClip;
		private var activeFloorNumber:int = -1;
		private var duration:Number = 0.3;
		private var _floors:Array;
		
		public function FloorLayoutBusiness() {
			super(ScreenName.FLOOR_LAYOUT, ScreenGroup.BUSINESS, "floorLayoutBusiness");
		}
		
		override protected function construct():void
		{
			super.construct();
			
			_floors = new Array();
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				_floors.push(MovieClip(this.getChildByName("bFloor" + i)));
			}
		}
		
		
		override protected function initUI():void
		{
			super.initUI();
			
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				MovieClip(_floors[i]).alpha = 0;
				initTextFloorButton(i);
			}
			
			showFloor(0);
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
			downloadPDFButton.addEventListener(MouseEvent.CLICK, downloadPDFButtonClick);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			backButton.removeEventListener(MouseEvent.CLICK, backButtonClick);
			downloadPDFButton.removeEventListener(MouseEvent.CLICK, downloadPDFButtonClick);
		}
		
		private function backButtonClick(event:MouseEvent):void {
			//this.dispatchEvent(new MenuEvent(MenuEvent.PREVIOUS, ""));
			controller.showScreen(ScreenName.RENDER);
		}
		
		private function downloadPDFButtonClick(event:MouseEvent):void {
			var url:String = "http://www.centar-bundek.hr/Content/PDF/Etaze/Poslovna-Etaza-" + activeFloorNumber + ".pdf";
			navigateToURL(new URLRequest(url), "_blank");
		}
		
		override protected function initTextFloorButton(floorNumber:int):void
		{
			var buttonName:String = "floor" + ((floorNumber >= 0) ? "" : "Minus") + Math.abs(floorNumber)  + "Button";
			var refMC:MovieClip = MovieClip(this.getChildByName(buttonName + "Ref"));
			var tfb:FloorLayoutFloorButton = new FloorLayoutFloorButton(floorNumber, buttonName);
			tfb.x = refMC.x;
			tfb.y = refMC.y;
			this.addChild(tfb);
			_floorButtons.push(tfb);
			
		}
		
		override public function showFloor(floorNumber:int):void {
			activeFloorNumber = floorNumber;
			TweenLite.to(currentFloor, duration, { alpha:0 } );
			TweenLite.to(MovieClip(_floors[floorNumber]), duration, { alpha:1 } );
			currentFloor = MovieClip(_floors[floorNumber]);
		}
	}

}